﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA.OpenAPI.Rss
{
    public class RssItemBuilder
    {
        private RssItem item;

        public RssItem Item
        {
            get { return item; }
        }

        public RssItemBuilder()
        {
            item = new RssItem();
        }

        public RssItemBuilder(string title, string desc = "", string link = "")
            : base()
        {
            SetBase(title, desc, link);
        }

        public void SetBase(string title, string desc = "", string link = "")
        {
            item.Title = title;
            if (!string.IsNullOrEmpty(desc)) item.Description = desc;
            if (!string.IsNullOrEmpty(link)) item.Link = link;
        }
        
        public void SetAuthor(string author)
        {
            item.Author = author;
        }

        public void SetComments(string url)
        {
            item.Comments = url;
        }

        public void SetGuid(string guid, bool? isPermlink = true)
        {
            item.Guid = new RssGuid()
            {
                Value = guid,
                IsPermaLink = isPermlink.Value
            };

        }

        public void SetPublished(DateTime date)
        {
            item.PubDate = date.ToString("r");
        }

        public RssCategory AddCategory(string text, string domain = "")
        {
            var cat = new RssCategory() {
            Text=text
            };
            if (!string.IsNullOrEmpty(domain))
                cat.Domain = domain;
            AddCategory(cat);
            return cat;
        }

        public void AddCategories(IEnumerable<string> categories)
        {
            foreach (var cat in categories)
                AddCategory(cat);
        }

        public void AddCategory(RssCategory category)
        {
            if (item.Categories == null)
                item.Categories = new List<RssCategory>();
            item.Categories.Add(category);
        }
    }
}
